<template>
  <div class="wrapper">
      <el-form :inline="true">
        <el-form-item>
          <date-phase ref="datePhase" dateTypeRange="1234" :datePhase="datePhase" ></date-phase>
        </el-form-item> 
        <el-form-item>
          <el-button type="primary" @click="searchItem()">查询</el-button>
          <el-button type="primary" @click="exportExcel()">导出</el-button>
        </el-form-item>
      </el-form>

      <div>
        <el-table :height="tableHeight" ref="table" :data="tableData"  border style="width: 100%" >
        <el-table-column label="时间" prop="date" min-width="180" align="center"></el-table-column>
        <el-table-column label="名称" prop="name" min-width="180" align="center"></el-table-column>
        <el-table-column label="整体业绩" prop="totalSale" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.totalSale? scope.row.totalSale: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="平台业绩" prop="platSale" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.platSale? scope.row.platSale: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="市场业绩" prop="marketSale" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.marketSale? scope.row.marketSale: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="PI数" prop="piNum" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.piNum? scope.row.piNum: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="新增客户数" prop="newCustomerNum" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.newCustomerNum? scope.row.newCustomerNum: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="本季度目标" prop="quarterGoal" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.quarterGoal? scope.row.quarterGoal: '0'}}
          </template>
        </el-table-column>
        <el-table-column label="今年目标" prop="yearGoal" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.yearGoal? scope.row.yearGoal: '0'}}
          </template>
        </el-table-column>
      </el-table>
      </div>
    </div>
</template>

<script>
  import dingMixin from  '@/mixins/ding'
  import formUtil from '@/utils/formUtil'
  import DatePhase from  '@/components/sale/date-phase'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'

  export default {
    data () {
      return {
        countryData: [],
        tableData: [],
        defaultProps: {
          children: 'list',
          label: 'name',
          isLeaf: 'leaf'
        },
        datePhase:{
          dateType: 2,
          value:'',
          to:''
        },
        platId: '',
        treeData: [],
        teamId: '',
        name: '',
        tableHeight: window.tableHeight,
        saleId: ''
      }
    },
    components: {
      DatePhase
    },
    activated() {
      this.tableHeight = document.documentElement['clientHeight'] - 220
      window.onresize = () => {
        this.tableHeight = document.documentElement['clientHeight'] - 220
      }
    },
    mixins: [dingMixin],
    async mounted(){
      this.teamId = this.$route.query.teamId;
      this.saleId = this.$route.query.saleId;
      this.name = this.$route.query.name;
      this.datePhase.to = '';
      this.datePhase.value = '';
      this.searchItem()
    },
    methods: {
      async searchItem() {
        this.tableData = await this.$httpN.postB('mpb/report/team2',{
          from: this.datePhase.value,
          to: this.datePhase.to,
          dateType: this.datePhase.dateType,
          teamId: this.teamId,
          countryCode: this.saleId,
        })
      },
      exportExcel(){
        this.$nextTick(function () {
          let wb = XLSX.utils.table_to_book(this.$refs.table.$el);
          let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
          let name = this.name ? this.name: '团队总'
          try {
            FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), `${name}成果报表.xlsx`)
          } catch (e) {
            if (typeof console !== 'undefined') console.log(e, wbout)
          }
          return wbout;
        });
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>